//
//  xianXiaDetailViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/9/26.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "xianXiaDetailViewController.h"
#import "HttpRequest.h"
#import "xianXiaTacketView.h"
#import "allActivity.h"
#import "yingErSwimViewController.h"
#import "BaoBeiPhotoViewController.h"
#import "yunFuPhotoViewController.h"
#import "zaoJiaoViewController.h"
#import "chanHouHuiFuViewController.h"
#import "xiaoErTuiNaViewController.h"
#import "xianXiaHuoDongViewController.h"
#import "UIImageView+WebCache.h"
#import "xianXiaDetail.h"
#import "Macros.h"
#import "UILabel+LabelHeightAndWidth.h"
#import "xianXiaTacketView.h"
#import "activityOrderViewController.h"
#import "nextXianXiaHuoDongViewController.h"
#import "adDetailViewController.h"

#import "SVProgressHUD.h"


@interface xianXiaDetailViewController ()
@property (weak, nonatomic) IBOutlet UIView *headerView01;
@property (weak, nonatomic) IBOutlet UIView *headerView02;
@property (weak, nonatomic) IBOutlet UIImageView *picImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *startTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *endTimeLabel;
@property (weak, nonatomic) IBOutlet UIView *nextView04;
@property (weak, nonatomic) IBOutlet UIView *backGroundView;
@property(nonatomic,copy)NSDictionary *json_dic;
@property(nonatomic,assign)float uview_y;
@property(nonatomic,assign)float uview_y01;
@property(nonatomic,assign)float uview_y02;

@property(nonatomic,retain)UILabel *detailLabel;
@property(nonatomic,retain)UIView *ticketView;
@property(nonatomic,assign)int number;
@property (weak, nonatomic) IBOutlet UIScrollView *backGroundScrollView;
@property(nonatomic,copy)NSString *urlString;
@property(nonatomic,copy)NSDictionary*action_dic;
@property(nonatomic,copy)NSArray *ticket_Array;
@end

@implementation xianXiaDetailViewController
@synthesize json_dic,number,detailLabel,urlString,action_dic,ticket_Array;
-(void)viewDidAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}
static float progress = 0.0f;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.backGroundView.hidden=YES;
//    self.view.hidden=YES;
    //设置背景色
    [self setBackGroundColor];
    //从服务器获取数据
    [self getDataFromNet];
    progress=0.0f;
    [SVProgressHUD showWithStatus:@"加载中请稍后..."];
}
//设置背景色
-(void)setBackGroundColor
{
    self.backGroundView.backgroundColor=customGrayColor;
    self.headerView01.backgroundColor=[UIColor colorWithRed:244/255.0 green:142/255.0 blue:177/255.0 alpha:1];
    self.headerView02.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1.0];
  
}
//从服务器获取数据
-(void)getDataFromNet
{
    allActivity *aa=[allActivity shardeManager];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];

    if (aa.num==100)
    {
        urlString=@"http://112.74.114.1/Parenting/index.php/api/Carousel/product";
        self.titleLabel.text=aa.product_nameString;
        parameters[@"product_name"]=aa.product_nameString;

    }else
    {
        urlString=@"http://112.74.114.1/Parenting/index.php/api/lineAction/detail";
        self.titleLabel.text=aa.titleString ;
        parameters[@"id"]=aa.idString;
    }
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         //获取数组
         json_dic=[json objectForKey:@"data"];
         action_dic =[NSDictionary dictionary];
         ticket_Array=[NSArray array];
         action_dic=[json_dic objectForKey:@"action"];
         ticket_Array=[json_dic objectForKey:@"ticket"];
//         xianXiaDetail *xxd=[xianXiaDetail shardeManager];
         if (action_dic.count>0)
         {
             [self dismiss];
//             xxd.picString=[action_dic objectForKey:@"pic"];
//             xxd.sellerString=[action_dic objectForKey:@"seller"];
//             xxd.addressString=[action_dic objectForKey:@"address"];
//             xxd.btimeString=[action_dic objectForKey:@"btime"];
//             xxd.detailString=[action_dic objectForKey:@"detail"];
//             xxd.etimeString=[action_dic objectForKey:@"etime"];
//             xxd.idString=[action_dic objectForKey:@"id"];
//             self.nameLabel.text=xxd.sellerString;
//             self.startTimeLabel.text=xxd.btimeString;
//             self.endTimeLabel.text=xxd.etimeString;
//             self.addressLabel.text=xxd.addressString;
//             self.uview_y=self.nextView04.frame.origin.y+self.nextView04.frame.size.height;
//             detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, self.uview_y, screenWidth, 20)];
//             detailLabel.textColor=[UIColor grayColor];
//             detailLabel.text=xxd.detailString;
//             detailLabel.font=[UIFont systemFontOfSize:14.0f];
//             detailLabel.backgroundColor=[UIColor whiteColor];
//             float height=[UILabel getHeightByWidth:screenWidth title:detailLabel.text font:detailLabel.font];
//             detailLabel.frame=CGRectMake(0, self.uview_y, screenWidth, height+15);
//             [self.backGroundView addSubview:detailLabel];
             

         }
//         NSURL* imagePath = [NSURL URLWithString:xxd.picString];
////         //给一张默认图片，先使用默认图片，当图片加载完成后再替换
//         [self.picImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
//        self.uview_y01=detailLabel.frame.size.height+detailLabel.frame.origin.y+12;
//         if (ticket_Array.count>0)
//         {
//             self.ticketView=[[UIView alloc]initWithFrame:CGRectMake(0, self.uview_y01, screenWidth, 42)];
//             self.ticketView.backgroundColor=[UIColor whiteColor];
//             [self.backGroundView addSubview:self.ticketView];
//             UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(12, 10, 100, 21)];
//             label.text=@"门票预订";
//             label.textColor=[UIColor blackColor];
//             label.font=[UIFont systemFontOfSize:17.0];
//             [self.ticketView addSubview:label];
//             self.uview_y02=self.ticketView.frame.origin.y+self.ticketView.frame.size.height;
//
//             for (int i=0; i<ticket_Array.count; i++)
//             {
//                 xxd.ticket_nameString=[ticket_Array[i] objectForKey:@"ticket_name"];
//                 xxd.ticket_priceString=[ticket_Array[i] objectForKey:@"ticket_price"];
//                 xxd.ticket_idString=[ticket_Array[i] objectForKey:@"ticket_id"];
//                 int num=[xxd.ticket_idString intValue];
//                 xianXiaTacketView *xxt=[xianXiaTacketView flagView:xxd];
////                 NSLog(@"xxd.ticket_nameString=%@",xxd.ticket_nameString);
//                 xxt.ticketLabel.text=xxd.ticket_nameString;
//                 xxt.priceLabel.text=[NSString stringWithFormat:@"¥  %@",xxd.ticket_priceString];
//                 xxt.frame=CGRectMake(0, self.uview_y02+82*i, screenWidth, 80);
////                 xxt.backgroundColor=[UIColor orangeColor];
//                 [xxt.orderBtn addTarget:self action:@selector(onOrder:) forControlEvents:UIControlEventTouchUpInside];
//                 xxt.priceLabel.textColor=pinkColor;
//                 xxt.orderBtn.tag=num;
//                 [xxt.orderBtn setTitleColor:pinkColor forState:UIControlStateNormal];
//                 //设置按钮的圆角半径不会被遮挡
//                 [xxt.orderBtn.layer setMasksToBounds:YES];
//                 //设置边界的宽度
//                 [xxt.orderBtn.layer setBorderWidth:1];
//                 [xxt.orderBtn.layer setBorderColor:pinkColor.CGColor];
//                 [self.backGroundView addSubview:xxt];
//             }
//         }
//         float allHeight=self.uview_y02+80*ticket_Array.count+20;
//         if (allHeight<screenHeight)
//         {
//             self.backGroundScrollView.contentSize=CGSizeMake(0, screenHeight);
//         }else{
//             self.backGroundScrollView.contentSize=CGSizeMake(0, allHeight);
//         }
//          NSLog(@"json_dic=%@",json_dic);
//         }
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
- (void)increaseProgress
{
    progress += 0.05f;
    [SVProgressHUD showProgress:progress status:@"加载中..."];
    
    if(progress < 1.0f){
        [self performSelector:@selector(increaseProgress) withObject:nil afterDelay:0.1f];
    } else {
        [self performSelector:@selector(dismiss) withObject:nil afterDelay:0.4f];
    }
}
- (void)dismiss {
    [SVProgressHUD dismiss];
    [self setNewView];
}
-(void)setNewView
{
    self.backGroundView.hidden=NO;
    xianXiaDetail *xxd=[xianXiaDetail shardeManager];
    xxd.picString=[action_dic objectForKey:@"pic"];
    xxd.sellerString=[action_dic objectForKey:@"seller"];
    xxd.addressString=[action_dic objectForKey:@"address"];
    xxd.btimeString=[action_dic objectForKey:@"btime"];
    xxd.detailString=[action_dic objectForKey:@"detail"];
    xxd.etimeString=[action_dic objectForKey:@"etime"];
    xxd.idString=[action_dic objectForKey:@"id"];
    self.nameLabel.text=xxd.sellerString;
    self.startTimeLabel.text=xxd.btimeString;
    self.endTimeLabel.text=xxd.etimeString;
    self.addressLabel.text=xxd.addressString;
    self.uview_y=self.nextView04.frame.origin.y+self.nextView04.frame.size.height;
    detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, self.uview_y, screenWidth, 20)];
    detailLabel.textColor=[UIColor grayColor];
    detailLabel.text=xxd.detailString;
    detailLabel.font=[UIFont systemFontOfSize:14.0f];
    detailLabel.backgroundColor=[UIColor whiteColor];
    float height=[UILabel getHeightByWidth:screenWidth title:detailLabel.text font:detailLabel.font];
    detailLabel.frame=CGRectMake(0, self.uview_y, screenWidth, height+15);
    [self.backGroundView addSubview:detailLabel];
    NSURL* imagePath = [NSURL URLWithString:xxd.picString];
    //         //给一张默认图片，先使用默认图片，当图片加载完成后再替换
    [self.picImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
    self.uview_y01=detailLabel.frame.size.height+detailLabel.frame.origin.y+12;
    self.ticketView=[[UIView alloc]initWithFrame:CGRectMake(0, self.uview_y01, screenWidth, 42)];
    self.ticketView.backgroundColor=[UIColor whiteColor];
    [self.backGroundView addSubview:self.ticketView];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(12, 10, 100, 21)];
    label.text=@"门票预订";
    label.textColor=[UIColor blackColor];
    label.font=[UIFont systemFontOfSize:17.0];
    [self.ticketView addSubview:label];
    self.uview_y02=self.ticketView.frame.origin.y+self.ticketView.frame.size.height;
    
    for (int i=0; i<ticket_Array.count; i++)
    {
        xxd.ticket_nameString=[ticket_Array[i] objectForKey:@"ticket_name"];
        xxd.ticket_priceString=[ticket_Array[i] objectForKey:@"ticket_price"];
        xxd.ticket_idString=[ticket_Array[i] objectForKey:@"ticket_id"];
        int num=[xxd.ticket_idString intValue];
        xianXiaTacketView *xxt=[xianXiaTacketView flagView:xxd];
        //                 NSLog(@"xxd.ticket_nameString=%@",xxd.ticket_nameString);
        xxt.ticketLabel.text=xxd.ticket_nameString;
        xxt.priceLabel.text=[NSString stringWithFormat:@"¥  %@",xxd.ticket_priceString];
        xxt.frame=CGRectMake(0, self.uview_y02+82*i, screenWidth, 80);
        //                 xxt.backgroundColor=[UIColor orangeColor];
        [xxt.orderBtn addTarget:self action:@selector(onOrder:) forControlEvents:UIControlEventTouchUpInside];
        xxt.priceLabel.textColor=pinkColor;
        xxt.orderBtn.tag=num;
        [xxt.orderBtn setTitleColor:pinkColor forState:UIControlStateNormal];
        //设置按钮的圆角半径不会被遮挡
        [xxt.orderBtn.layer setMasksToBounds:YES];
        //设置边界的宽度
        [xxt.orderBtn.layer setBorderWidth:1];
        [xxt.orderBtn.layer setBorderColor:pinkColor.CGColor];
        [self.backGroundView addSubview:xxt];
    }

     float allHeight=self.uview_y02+80*ticket_Array.count+20;
     if (allHeight<screenHeight)
     {
         self.backGroundScrollView.contentSize=CGSizeMake(0, screenHeight);
     }else{
         self.backGroundScrollView.contentSize=CGSizeMake(0, allHeight);
     }
}
-(void)onOrder:(UIButton*)sender
{
    //跳转到下一界面
    xianXiaDetail *xxd=[xianXiaDetail shardeManager];
    xxd.ticket_idString=[NSString stringWithFormat:@"%ld",sender.tag];
    xxd.toTheNextView=1;
    activityOrderViewController *aovc=[activityOrderViewController new];
    [self.navigationController pushViewController:aovc animated:YES];
//    NSLog(@"订购了");

}
- (IBAction)goBack:(id)sender
{
    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
//    NSString *num=[NSString stringWithFormat:@"%d",aa.num];
//    [userDefaults setObject:num forKey:@"aa.num"];
    NSString *numString=[userDefaults objectForKey:@"aa.num"];
    int num=[numString intValue];
//    NSLog(@"num=%d",num);
    if (num==10)
    {
        [userDefaults setObject:@"0" forKey:@"aa.num"];
        //        //跳到指定的界面
        for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:[nextXianXiaHuoDongViewController class]])
            {
                [self.navigationController popToViewController:controller animated:YES];
                
            }
        }
    }else if (num==100)
    {
        for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:[adDetailViewController class]])
            {
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    }
    else{
//        //跳到指定的界面
        for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:[xianXiaHuoDongViewController class]])
            {
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    }
//    else if (number==3)
//    {
//        //跳到指定的界面
//        for (UIViewController *controller in self.navigationController.viewControllers)
//        {
//            if ([controller isKindOfClass:[BaoBeiPhotoViewController class]])
//            {
//                [self.navigationController popToViewController:controller animated:YES];
//            }
//        }
//    }
//    else if (number==4)
//    {
//        //跳到指定的界面
//        for (UIViewController *controller in self.navigationController.viewControllers)
//        {
//            if ([controller isKindOfClass:[yunFuPhotoViewController class]])
//            {
//                [self.navigationController popToViewController:controller animated:YES];
//            }
//        }
//    }
//    else if (number==5)
//    {
//        //跳到指定的界面
//        for (UIViewController *controller in self.navigationController.viewControllers)
//        {
//            if ([controller isKindOfClass:[zaoJiaoViewController class]])
//            {
//                [self.navigationController popToViewController:controller animated:YES];
//            }
//        }
//    
//    }
//    else if (number==6)
//    {
//        //跳到指定的界面
//        for (UIViewController *controller in self.navigationController.viewControllers)
//        {
//            if ([controller isKindOfClass:[chanHouHuiFuViewController class]])
//            {
//                [self.navigationController popToViewController:controller animated:YES];
//            }
//        }
//    }
//    else if (number==7)
//    {
//        //跳到指定的界面
//        for (UIViewController *controller in self.navigationController.viewControllers)
//        {
//            if ([controller isKindOfClass:[xiaoErTuiNaViewController class]])
//            {
//                [self.navigationController popToViewController:controller animated:YES];
//            }
//        }
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
